package com.cdtu;

import com.cdtu.utils.FileUtils;
import com.cdtu.utils.NetUtils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.json.XML;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

@Slf4j
public class MessageController {
    /**
     * 触发报警类型：fielddetection,regionEntrance
     *
     * @param eventObj
     * @throws IOException
     */
    public static void txtScan(JSONObject eventObj) throws IOException {
        JSONObject eventNotificationAlert = eventObj.getJSONObject("EventNotificationAlert");
//        log.info("报文:\r\n {}", eventNotificationAlert.toString());
        String eventType = FileUtils.getProp("eventType");

        // 读取配置文件
        JsonNode root = FileUtils.getJsonData("AILogiController.json");
        ArrayNode arrayNode = (ArrayNode) root.get("send2list");
        log.debug("配置文件中的报警类型 {}", eventType);
        // 条件：事件类型 ==> 区域检测
        if (eventType.contains(eventNotificationAlert.getString("eventType"))) {
            log.info("=========检测到报警类型: {} =================",
                    eventNotificationAlert.getString("eventType"));
            // 查找分站设备
            log.info("正在通知分站设备...");
            for (JsonNode node : arrayNode) {
                String channelID = node.get("channelID").asText();
                String IP = node.get("IP").asText();
                String port = node.get("port").asText();
                String chainID = node.get("chainID").asText();
                int repeatTimes = Integer.parseInt(node.get("repeatTimes").asText());
                int restoreTimes = Integer.parseInt(node.get("restoreTimes").asText());

                log.debug("xml 通道 ID : {}  ===> json 通道 ID: {}", eventNotificationAlert.getString("channelID"), channelID);
                // 条件：通道 ID
                if (channelID.equals(eventNotificationAlert.getString("channelID"))) {
                    log.info("通道ID: {}, IP: {}", channelID, IP);
                    String[] controlOrders = Order.generateOrder(1, node);
                    log.info("给通道 {} {} 发送控制命令: {}", channelID, IP, Arrays.toString(controlOrders));
                    // 条件：主机 IP
                    NetUtils.sendStringToClients(IP, controlOrders);
                    // 发送控制命令成功，每隔 15 秒发恢复命令，共发 5 次
                    for (long i = 0; i < repeatTimes; i++) {
                        // 恢复算法
                        long delay = (long) (restoreTimes + (i * 2));
                        NettyServer.scheduler.schedule(() -> {
                            String[] restoreOrders = Order.generateOrder(0, node);
                            // AILogiController.json 里的 IP 和 port
                            log.info("给通道 {} {} 发送恢复命令: {}", channelID, IP, Arrays.toString(controlOrders));
                            NetUtils.sendStringToClients(IP, restoreOrders);
                        }, delay, TimeUnit.SECONDS);
                    }

                }
            }
        }
    }

    // xml 格式转 json 格式
    public static JSONObject xmlToJSON3(String xmlStr) {
        org.json.JSONObject jsonObject2 = XML.toJSONObject(xmlStr, true);
        log.debug(jsonObject2.toString());
        return jsonObject2;
    }
}
